package hotel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.GregorianCalendar;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class CreaPDF {
	public static GregorianCalendar gc=new GregorianCalendar();
	public static int giorno=gc.get(GregorianCalendar.DATE);
	public static int mese=gc.get(GregorianCalendar.MONTH)+1;
	public static int anno=gc.get(GregorianCalendar.YEAR);
	public static String oggi=giorno+"-"+mese+"-"+anno;
	public static Database d;
	private static ImageIcon iconav = new ImageIcon("Risorse/iconav.png");
	
	public CreaPDF(Database db) throws FileNotFoundException, DocumentException{
		d=db;
		apriFinestra();
	}
	
	public static void apriFinestra() throws DocumentException {
		JFileChooser fc=new JFileChooser();
		fc.setApproveButtonText("Crea PDF");
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		File file;
		String indirizzo;
		if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
			indirizzo = file.getPath()+"/clienti-"+oggi+".pdf";
		}else
			return;
		Document document = new Document();
		try {
			PdfWriter.getInstance(document, new FileOutputStream(indirizzo));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		document.open();
		document.addTitle("Clienti Hotel");
		document.addSubject("Clienti Hotel");
		document.addKeywords("clienti,hotel");
		document.addAuthor("GestionaleHotel");
		Chapter chapter = new Chapter(new Paragraph("Clienti registrati"), 1);
		Paragraph sectionParagraph = new Paragraph(oggi);
		Section section = chapter.addSection(sectionParagraph);
		creaTabella(section);
		document.add(chapter);
		document.close();
		JOptionPane.showMessageDialog(null,"Pdf Esportato con successo!","Ok!", JOptionPane.INFORMATION_MESSAGE, iconav);
		
	}
	
	private static void creaTabella(Section section) {
		PdfPTable tabella = new PdfPTable(8);
		 
		  PdfPCell c1 = new PdfPCell(new Phrase("Nome"));
		  c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		  c1.setGrayFill(0.8f);
		  tabella.addCell(c1);
		 
		  c1 = new PdfPCell(new Phrase("Cognome"));
		  c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		  c1.setGrayFill(0.8f);
		  tabella.addCell(c1);
		 
		  c1 = new PdfPCell(new Phrase("Data di nascita"));
		  c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		  c1.setGrayFill(0.8f);
		  tabella.addCell(c1);
		  
		  c1 = new PdfPCell(new Phrase("Comune di nascita"));
		  c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		  c1.setGrayFill(0.8f);
		  tabella.addCell(c1);
		  
		  c1 = new PdfPCell(new Phrase("Prov."));
		  c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		  c1.setGrayFill(0.8f);
		  tabella.addCell(c1);
		  
		  c1 = new PdfPCell(new Phrase("Tipo Documento"));
		  c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		  c1.setGrayFill(0.8f);
		  tabella.addCell(c1);
		  
		  c1 = new PdfPCell(new Phrase("Numero"));
		  c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		  c1.setGrayFill(0.8f);
		  tabella.addCell(c1);
		  
		  c1 = new PdfPCell(new Phrase("E-mail"));
		  c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		  c1.setGrayFill(0.8f);
		  tabella.addCell(c1);
		  tabella.setHeaderRows(1);
		 
		  riempi(tabella);
		 
		  section.add(tabella);
	}
	
	private static void riempi(PdfPTable tabella) {
		try {
			ResultSet rs=d.stat.executeQuery("SELECT nome,cognome,DATE_FORMAT(datadinascita,'%d/%m/%Y'),comunedinascita,provinciadinascita,tipodidocumento,numerodidocumento,email from clienti");
			while(rs.next()){
				tabella.addCell(rs.getString(1));
				tabella.addCell(rs.getString(2));
				tabella.addCell(rs.getString(3));
				tabella.addCell(rs.getString(4));
				tabella.addCell(rs.getString(5));
				tabella.addCell(rs.getString(6));
				tabella.addCell(rs.getString(7));
				tabella.addCell(rs.getString(8));
			}
			tabella.completeRow();
		} catch (SQLException e){
			e.printStackTrace();
		}
	}

}
